<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<html>
<head>
    <title>Title</title>
    <script src="${pageContext.request.contextPath}/js/jquery-3.4.0.js"></script>
    <script>
        $(function () {
            $("#addBtn").click(function () {
                window.location.href="${pageContext.request.contextPath}/add.jsp";
            })
        });
        function delPro(pid) {
            if(confirm("您确定要删除？")){
                window.location.href = "${pageContext.request.contextPath}/productServlet?method=delProById&pid="+pid;
            }
        };
        $(function () {
            $("[type=checkbox]:first").change(function () {
                $("[type=checkbox]:gt(0)").prop("checked",this.checked);
            });
            $("[type=checkbox]:gt(0)").change(function () {
                var num1 = $("[type=checkbox]:gt(0)").length;
                var num2 = $("[type=checkbox]:gt(0):checked").length;
                if(num1==num2){
                    $("[type=checkbox]:first").prop("checked",true);
                }else{
                    $("[type=checkbox]:first").prop("checked",false);
                }
            });

            $("#delBatch").click(function () {
               var params =  $("[type=checkbox]:gt(0):checked").serialize();
               if(params==null||params.trim().length==0){
                   alert("请选择要删除的项！")
                   return;
               }
               if(confirm("您确定要删除？")){
                   window.location.href = "${pageContext.request.contextPath}/productServlet?method=batchDelete&"+params;
               }

            })

        });
    </script>
</head>
<body>
    <table border="1" width="1200" style="border-collapse: collapse;text-align: center" >
        <tr>
            <th><input type="checkbox" id="allDel"></th>
            <th>序号</th>
            <th>名称</th>
            <th>市场价格</th>
            <th>零售价格</th>
            <th>出产日期</th>
            <th>详细描述</th>
            <th colspan="2" width="10%">操作</th>
        </tr>
    <c:forEach items="${pros}" var="pro" varStatus="sta">
        <tr>
            <td><input type="checkbox" name="pid" value="${pro.pid}"></td>
            <td>${sta.count}</td>
            <td>${pro.pname}</td>
            <td>${pro.market_price}</td>
            <td>${pro.shop_price}</td>
            <td width="16%">
            <fmt:formatDate value="${pro.pdate}" pattern="yyyy-MM-dd HH:mm:ss"></fmt:formatDate>
            </td>
            <c:if test="${fn:length(pro.pdesc)>25}">
                <td title="${pro.pdesc}">
                        ${fn:substring(pro.pdesc,0 ,25)}...
                </td>
            </c:if>
            <c:if test="${fn:length(pro.pdesc)<=25}">
                <td>
                        ${pro.pdesc}
                </td>
            </c:if>
            <td><a onclick="delPro('${pro.pid}')" href="javascript:void(0)">删除</a></td>
            <td><a href="${pageContext.request.contextPath}/productServlet?method=findProById&pid=${pro.pid}">修改</a></td>
        </tr>

    </c:forEach>

    </table>
    <input type="button" id="addBtn" value="新增商品">
    <input type="button" id="delBatch" value="批量删除商品">
</body>
</html>
